package com.wswy.wzcx.ui.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewCompat;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;
import com.che.libcommon.ui.BaseFragment;
import com.wswy.wzcx.AppContext;
import com.wswy.wzcx.R;
import com.wswy.wzcx.api.ConfigsKt;
import com.wswy.wzcx.model.home.RPaths;
import com.wswy.wzcx.model.home.RouterUtils;
import com.wswy.wzcx.module.ad.AdConfigLoader;
import com.wswy.wzcx.statistics.StatTools;
import com.wswy.wzcx.statistics.StatisticsId;
import com.wswy.wzcx.ui.data.JZCFResult;
import com.wswy.wzcx.ui.web.WebViewActivity;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

/**
 * Created by wangduheng26 on 2018/3/19.
 * wangduheng26@gmail.com
 */

public class JZCFResultFragment extends BaseFragment {
    public static final String TAG = "JZCFResultFragment";

    public static JZCFResultFragment newInstance(JZCFResult mode) {

        Bundle args = new Bundle();
        args.putParcelable("data", mode);
        JZCFResultFragment fragment = new JZCFResultFragment();
        fragment.setArguments(args);
        return fragment;
    }

    private TextView tvResult;
    private TextView tvDays;
    private TextView tvDaysLimit;

    private JZCFResult mode;


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        Bundle arguments = getArguments();
        if (arguments != null && arguments.containsKey("data")) {
            mode = arguments.getParcelable("data");
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_jzcf_result, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        tvResult = view.findViewById(R.id.tv_result);
        tvDays = view.findViewById(R.id.tv_days);
        tvDaysLimit = view.findViewById(R.id.tv_days_limit);
        view.findViewById(R.id.rl_jzxg).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                WebViewActivity.startWebview(getContext(), "驾驶证新规", ConfigsKt.H5_JSZXG(), true);
            }
        });

        ViewCompat.setElevation(view.findViewById(R.id.f_round), 5);


    }

    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (mode != null) {
            tvResult.setText(mode.getScore());
            Calendar instance = Calendar.getInstance();
            instance.add(Calendar.DAY_OF_YEAR, mode.getClearDay());
            tvDaysLimit.setText(TextUtils.concat(format.format(instance.getTime()), "清零"));
            tvDays.setText(TextUtils.concat(String.valueOf(mode.getClearDay()), "天"));


            View.OnClickListener clickListener = new View.OnClickListener(){

                @Override
                public void onClick(View v) {

                    int id = v.getId();
                    Context context = v.getContext();

                    Intent intent = null;

                    if (id == R.id.rl_wzdb){
                        intent = RouterUtils.getTargetIntent(context, RPaths.wzdb);

                        StatTools.sendShow(AppContext.getContext(), StatisticsId.jzcf_bottom_click,"违章查缴");

                    }else if (id == R.id.rl_fkdj){
                        intent = RouterUtils.getTargetIntent(context,RPaths.fkdj);
                        StatTools.sendShow(AppContext.getContext(), StatisticsId.jzcf_bottom_click,"罚单缴费");

                    }else if (id == R.id.rl_acgz){
                        intent = RouterUtils.getWebIntent(context,"http://qd.jingzhengu.com/xiansuo/sellcar-shoujiweizhang.html");

                        StatTools.sendShow(AppContext.getContext(), StatisticsId.jzcf_bottom_click,"爱车估值");

                    }else if (id == R.id.rl_jryj){
                        intent = RouterUtils.getTargetIntent(context,RPaths.OIL_PRICES);

                        StatTools.sendShow(AppContext.getContext(), StatisticsId.jzcf_bottom_click,"油价趋势");

                    }
                    if (intent != null){
                        startActivity(intent);
                    }

                }
            };

            getView().findViewById(R.id.rl_wzdb).setOnClickListener(clickListener);
            getView().findViewById(R.id.rl_fkdj).setOnClickListener(clickListener);
            getView().findViewById(R.id.rl_acgz).setOnClickListener(clickListener);
            getView().findViewById(R.id.rl_jryj).setOnClickListener(clickListener);

        }
    }

    @Override
    public String getFragmentTitle() {
        return "驾驶证查分";
    }
}
